package com.msconfig.mvp.view.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.geo.msconfig.R;
import com.geo.parse.GnssSateInfoList;
import com.msconfig.device.GeoLocationData;
import com.msconfig.event.DeviceUpdateEvent;
import com.msconfig.mvp.view.activity.base.BaseMvpFragment;
import com.msconfig.mvp.view.activity.base.BasePresenter;
import com.msconfig.mvp.view.adapter.SatelliteFormAdapter;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

public class SatelliteFormFragment extends BaseMvpFragment implements View.OnClickListener {
    private ImageView ivCommonBack;
    private TextView tvCommonTitle;
    private ListView listView;
    private SatelliteFormAdapter adapter;
    private int firstVisibleId;
    private int firstVisiblePositionTop;
    private GnssSateInfoList mGnssSateInfoList;
    private GnssSateInfoList mSateInfoList;
    private boolean isDisconnect=false;


    public static SatelliteFormFragment newInstance() {
        Bundle args = new Bundle();
        SatelliteFormFragment fragment = new SatelliteFormFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.layout_fragment_satellite_form;
    }

    @Override
    protected void initView(View view) {
        ivCommonBack=view.findViewById(R.id.iv_common_back);
        tvCommonTitle=view.findViewById(R.id.tv_common_title);
        listView=view.findViewById(R.id.lv_satellite_form);
    }

    @Override
    protected void initListener(View view) {
        ivCommonBack.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        tvCommonTitle.setText(R.string.title_form);
        mSateInfoList=new GnssSateInfoList();
        adapter=new SatelliteFormAdapter(getContext(), mSateInfoList);
        listView.setAdapter(adapter);
        //记住上一次滚动时的位置信息
        listView.setOnScrollListener(new AbsListView.OnScrollListener(){
            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                firstVisibleId = firstVisibleItem;
                View item = listView.getChildAt(0);
                firstVisiblePositionTop = (item == null) ? 0 : item.getTop();
            }
            @Override
            public void onScrollStateChanged(AbsListView view , int scrollState){
            }
        });

    }

    //卫星信息更新
    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
    public void showReceiveDataSuccess(DeviceUpdateEvent.UpdateSatelliteResultStatus event) {
        mGnssSateInfoList= GeoLocationData.getInstance().getSateInfoList();
        //要使用mSateInfoList的同一对象，不能给新对象mGnssSateInfoList不然不能够notifyDataSetChanged
        mSateInfoList.setSateInfoList(mGnssSateInfoList.getSateInfoList());
        //解决断开连接重连显示问题
        if(isDisconnect){
            listView.setAdapter(adapter);
            isDisconnect=false;
            return;
        }
        adapter.notifyDataSetChanged();
        //上一次滑动的可见的位置，这个位置Y轴的偏移量
        listView.setSelectionFromTop(firstVisibleId, firstVisiblePositionTop);

    }


    //断开设备
    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
    public void showDeviceDisConnectResultStatus(DeviceUpdateEvent.DeviceDisConnectResultStatus event) {
        isDisconnect=true;
        listView.setAdapter(null);
    }

    @Override
    public void onClick(View v) {
        if(v==ivCommonBack){
            backTo();
        }

    }

    @Override
    protected void createPresenter(List<BasePresenter> mPresenters) {

    }
}
